package chapter14.core;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;


public class BaseAction {
    public static final int AUTOWIRE_NO = 0;
    public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
    public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
    protected int autowireMode = AUTOWIRE_BY_NAME;
    protected boolean dependencyCheck = true;

    public BaseAction() {
        this.init();
    }

    // spring
    protected void init() {
        getApplicationContext().getBeanFactory()
            .autowireBeanProperties(this, this.autowireMode,
            this.dependencyCheck);
    }

    protected ConfigurableApplicationContext getApplicationContext() {
        return (ConfigurableApplicationContext) CtxListener.getCtx();
    }

    // session
    protected Object getSessionValue(String name) {
        return HttpContextHolder.get().request.getSession()
                                              .getAttribute(name);
    }

    protected void setSessionValue(String name, Object value) {
        HttpContextHolder.get().request.getSession()
                                       .setAttribute(name, value);
    }
}
